package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author moningxi
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;
    @Resource
    private MinIOFileStorageService storageService;
    @Resource
    private DishMapper dishMapper;

    /**
     * 新增套餐和相应菜品
     */
    @Override
    @Transactional
    public void saveSetmealWithDishes(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 保存套餐
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.save(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(e -> e.setSetmealId(setmeal.getId()));

        if (!setmealDishes.isEmpty()) {
            // 保存套餐菜品
            setmealDishMapper.saveBatch(setmealDishes);
        }
    }

    /**
     * 套餐分页查询
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 套餐起售、停售
     */
    @Override
    public void startOrStop(int status, Long id) {
        List<Long> ids = setmealDishMapper.getDishIdsBySetmealId(id);
        for (Long dishId : ids) {
            Dish dish = dishMapper.getById(dishId);
            if (StatusConstant.DISABLE.equals(dish.getStatus())) {
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.startOrStop(status, id);
    }

    /**
     * 根据id查询套餐
     */
    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐和相应菜品
     */
    @Override
    @Transactional
    public void updateWithSetmealDish(SetmealDTO setmealDTO) {
        Setmeal dbSetmeal = setmealMapper.getById(setmealDTO.getId());
        if (dbSetmeal == null) {
            throw new BaseException(MessageConstant.UNKNOWN_ERROR);
        }
        if (StringUtils.isNotBlank(dbSetmeal.getImage()) && !dbSetmeal.getImage().equals(setmealDTO.getImage())) {
            // 删除图片
            storageService.delete(dbSetmeal.getImage());
        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 更新套餐信息
        setmealMapper.update(setmeal);

        // 更新套餐菜品信息，先删除在新增
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(e -> e.setSetmealId(setmealDTO.getId()));
        setmealDishMapper.saveBatch(setmealDishes);
    }

    /**
     * 批量删除套餐和对应菜品
     */
    @Override
    @Transactional
    public void deleteWithSetmealDish(List<Long> ids) {
        List<Setmeal> setmeals = new ArrayList<>();
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            // 判断是否起售
            if (StatusConstant.ENABLE.equals(setmeal.getStatus())) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            setmeals.add(setmeal);
        }

        // 删除图片
        setmeals.forEach(e -> {
            if (StringUtils.isNotBlank(e.getImage())) {
                storageService.delete(e.getImage());
            }
        });

        // 删除套餐
        setmealMapper.deleteBatch(ids);

        // 删除对应菜品
        setmealDishMapper.deleteBatchBySetmealId(ids);
    }

    /**
     * 条件查询
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
